package actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import searcher.enRentSearcher;

import com.opensymphony.xwork2.ActionContext;

import entity.Rent;

public class SearchThree extends BaseAction {
	private String ifUseXML;
	private ArrayList<Rent> rlt;

	public String execute() throws Exception {
		String deco = this.getDecoration();
		String floo = this.getFloor();
		String regi = this.getRegion();
		String xml = this.getIfUseXML();

		String lbStr = this.getLowerBound();
		String ubStr = this.getUpperBound();
		if (lbStr != null && lbStr.length() == 0) {
			lbStr = "-1";
		}
		if (ubStr != null && ubStr.length() == 0) {
			ubStr = "-1";
		}
		if (deco != null && deco.length() == 0) {
			deco = null;
		}
		if (floo != null && floo.length() == 0) {
			floo = null;
		}
		if (regi != null && regi.length() == 0) {
			regi = null;
		}

		enRentSearcher temp = new enRentSearcher(Integer.parseInt(lbStr),
				Integer.parseInt(ubStr), deco, floo, regi);
		int pageCount = temp.preenRentSearch();

		String curPage = this.getPage();
		int pagenum = 1;
		if (null != curPage) {
			pagenum = Integer.parseInt(curPage) > 1 ? Integer.parseInt(curPage)
					: 1;
			pagenum = pagenum <= pageCount ? pagenum : pageCount;

		}
		if ((xml != null && xml.compareTo("否") == 0) || xml == null || xml.length() == 0) {
			ActionContext.getContext().getSession().put("page", pagenum);
			ActionContext.getContext().put("rlts",
					temp.getResultatPage(pagenum));

			ActionContext.getContext().put("lowerBound", this.getLowerBound());
			ActionContext.getContext().put("upperBound", this.getUpperBound());
			ActionContext.getContext().put("region", this.getRegion());
			ActionContext.getContext().put("floor", this.getFloor());
			ActionContext.getContext().put("decoration", this.getDecoration());
			return "input";

		} else if (xml.compareTo("是") == 0) {
			rlt = temp.getResultatPage(pagenum);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/xml;charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			return SUCCESS;
		}
		return SUCCESS;
	}

	public String getLowerBound() {
		return lowerBound;
	}

	public void setLowerBound(String lowerBound) {
		this.lowerBound = lowerBound;
	}

	public String getUpperBound() {
		return upperBound;
	}

	public void setUpperBound(String upperBound) {
		this.upperBound = upperBound;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getFloor() {
		return floor;
	}

	public void setFloor(String floor) {
		this.floor = floor;
	}

	public String getDecoration() {
		return decoration;
	}

	public void setDecoration(String decoration) {
		this.decoration = decoration;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getIfUseXML() {
		return ifUseXML;
	}

	public void setIfUseXML(String ifUseXML) {
		this.ifUseXML = ifUseXML;
	}

	public ArrayList<Rent> getRlt() {
		return rlt;
	}

	public void setRlt(ArrayList<Rent> rlt) {
		this.rlt = rlt;
	}

	private String lowerBound;
	private String upperBound;
	private String region;
	private String floor;
	private String decoration;
	private String page;

}
